<?php

//就近 www.jiujin.me
global $_W, $_GPC;

$agentlevels = $this->model->getLevels();
$operation = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($operation == 'display') {
    ca('commission.agent.view');
    $level = $this->set['level'];
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $params = array();
    $condition = '';
    if (!empty($_GPC['mid'])) {
        $condition .= ' and dm.id=:mid';
        $params[':mid'] = intval($_GPC['mid']);
    }
    if (!empty($_GPC['status'])) {
        $condition .= ' and dm.status=:status';
        $params[':status'] = intval($_GPC['status']);
    }
    if (!empty($_GPC['realname'])) {
        $_GPC['realname'] = trim($_GPC['realname']);
        $condition .= ' and ( dm.realname like :realname or dm.nickname like :realname or dm.mobile like :realname)';
        $params[':realname'] = "%{$_GPC['realname']}%";
    }
    if ($_GPC['parentid'] == '0') {
        $condition .= ' and dm.agentid=0';
    } else {
        if (!empty($_GPC['parentname'])) {
            $_GPC['parentname'] = trim($_GPC['parentname']);
            $condition .= ' and ( p.mobile like :parentname or p.nickname like :parentname or p.realname like :parentname)';
            $params[':parentname'] = "%{$_GPC['parentname']}%";
        }
    }
    if ($_GPC['followed'] != '') {
        if ($_GPC['followed'] == 2) {
            $condition .= ' and f.follow=0 and dm.uid<>0';
        } else {
            $condition .= ' and f.follow=' . intval($_GPC['followed']);
        }
    }
    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        if ($_GPC['searchtime'] == '1') {
            $condition .= " AND dm.agenttime >= :starttime AND dm.agenttime <= :endtime ";
            $params[':starttime'] = $starttime;
            $params[':endtime'] = $endtime;
        }
    }
    if (!empty($_GPC['agentlevel'])) {
        $condition .= ' and dm.agentlevel=' . intval($_GPC['agentlevel']);
    }
    $list = pdo_fetchall("select dm.*,dm.nickname,dm.avatar,l.levelname,p.nickname as parentname,p.avatar as parentavatar from " . tablename('ewei_shop_member') . " dm " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('ewei_shop_commission_level') . " l on l.id = dm.agentlevel" . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid and f.uniacid={$_W['uniacid']}" . " where dm.uniacid = " . $_W['uniacid'] . " and dm.isagent =1  {$condition} ORDER BY dm.agenttime desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
    $total = pdo_fetchcolumn("select count(dm.id) from" . tablename('ewei_shop_member') . " dm  " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid" . " where dm.uniacid =" . $_W['uniacid'] . " and dm.isagent =1 {$condition}", $params);
    foreach ($list as &$row) {
        $info = $this->model->getInfo($row['openid'], array('total', 'pay'));
        $row['levelcount'] = $info['agentcount'];
        $row['levelcount']=$row['levelcount']-$no_guanzhu;
        if ($level >= 1) {
            $row['level1'] = $info['level1'];
        }
        if ($level >= 2) {
            $row['level2'] = $info['level2'];
        }
        if ($level >= 3) {
            $row['level3'] = $info['level3'];
        }
        $row['credit1'] = m('member')->getCredit($row['openid'], 'credit1');
        $row['credit2'] = m('member')->getCredit($row['openid'], 'credit2');
        $row['commission_total'] = $info['commission_total'];
        $row['commission_pay'] = $info['commission_pay'];
        $row['followed'] = m('user')->followed($row['openid']);
    }
    unset($row);
    $pager = pagination($total, $pindex, $psize);
} else {
    if ($operation == 'detail') {
        ca('commission.agent.view');
        $id = intval($_GPC['id']);
        $member = $this->model->getInfo($id, array('total', 'pay'));
        //gmm 2015-12-21 招募后台审核 下拉框纠正
        if ($member['zhaomu'] == 1 && $member['agentid']) {
            $parent = pdo_fetch("select agentlevel from" . tablename('ewei_shop_member') . " where uniacid =" . $_W['uniacid'] . " and id = " . $member['agentid']);
            if ($parent['agentlevel'] == 1) {  //战略
                $member['agentlevel'] = 2;
            } else if ($parent['agentlevel'] == 2) {
                $member['agentlevel'] = 3;
            } else {
                
            }
        }
        $goodses = pdo_fetch('SELECT goodsids FROM ' . tablename('ewei_shop_commission_shop') . " WHERE uniacid = '{$_W['uniacid']}' AND mid = '{$id}'");
        if (!empty($goodses)) {
            $goodsarr = explode(',', $goodses['goodsids']);
        }
        $goods = pdo_fetchall('SELECT id,title FROM ' . tablename('ewei_shop_goods') . " WHERE uniacid = '{$_W['uniacid']}' and deleted = 0 and status=1 order by id desc");
        if (!empty($goodsarr)) {
            foreach ($goods as &$g) {
                if (in_array($g['id'], $goodsarr)) {
                    $g['goodsid_select'] = 1;
                }
            }
        }
        if (checksubmit('submit')) {
            ca('commission.agent.edit|commission.agent.check');
            $data = is_array($_GPC['data']) ? $_GPC['data'] : array();
            /*
             *  gmm
             *  修改：审核通过 zhaomu变成2
             *  时间：2015年11月25日
             */
            if ($data['status'] == 1) {
                $data['zhaomu'] = 2;
            } else {
                $data['zhaomu'] = 1;
            }
            if (empty($_GPC['oldstatus']) && $data['status'] == 1) {
                $time = time();
                $data['agenttime'] = time();
                //gmm 2015-12-19 欢迎语
                if ($member['agentlevel'] == 1) {
                    $this->model->sendMessage($member['openid'], array('nickname' => $member['nickname'], 'agenttime' => $time), TM_COMMISSION_BECOME1);
                } else if ($member['agentlevel'] == 2) {
                    $this->model->sendMessage($member['openid'], array('nickname' => $member['nickname'], 'agenttime' => $time), TM_COMMISSION_BECOME2);
                } else if ($member['agentlevel'] == 3) {
                    $this->model->sendMessage($member['openid'], array('nickname' => $member['nickname'], 'agenttime' => $time), TM_COMMISSION_BECOME3);
                } else {
                    $this->model->sendMessage($member['openid'], array('nickname' => $member['nickname'], 'agenttime' => $time), TM_COMMISSION_BECOME);
                }
                plog('commission.agent.check', "审核分销商 <br/>分销商信息:  ID: {$member['id']} /  {$member['openid']}/{$member['nickname']}/{$member['realname']}/{$member['mobile']}");
            }
            $user = pdo_fetch("select * from " . tablename('ewei_shop_agent') . "  where uniacid=:uniacid  and mid=:mid  ", array(':uniacid' => $_W['uniacid'], ':mid' => $id));
            if ($user) {
                    if($data['isShop']==1 && empty($user['opentime'])){
                    pdo_update("ewei_shop_agent", array('opentime'=>time()), array('uniacid' => $_W['uniacid'], 'mid' => $id));
                    }
                    if($data['isShop']==0){
                    pdo_update("ewei_shop_agent", array('opentime'=>""), array('uniacid' => $_W['uniacid'], 'mid' => $id));
                    }
            }else{
                if($data['isShop']==1){
                    $shop = array('uniacid' => $_W['uniacid'],'mid' => $id,'createtime' => time(),'opentime'=>time());
                }else{
                    $shop = array('uniacid' => $_W['uniacid'],'mid' => $id,'createtime' => time());
                }
                pdo_insert("ewei_shop_agent", $shop);
            }
            if (!empty($_GPC['good'])) {
                $goodsid = implode(',', $_GPC['good']);
            } else {
                $goodsid = "";
            }
            $xiaoshop = pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $member['id']));
            $shopdata['selectgoods'] = 1;
            $shopdata['selectcategory'] = 0;
            $shopdata['uniacid'] = $_W['uniacid'];
            $shopdata['mid'] = $member['id'];
            $shopdata['goodsids'] = $goodsid;
            if (empty($xiaoshop['id'])) {
                pdo_insert('ewei_shop_commission_shop', $shopdata);
            } else {
                pdo_update('ewei_shop_commission_shop', $shopdata, array('id' => $xiaoshop['id']));
            }
            //pdo_update('ewei_shop_commission_shop',array('goodsids'=>$goodsid),array('uniacid' =>$_W['uniacid'],'mid'=>$id));
            plog('commission.agent.edit', "修改分销商 <br/>分销商信息:  ID: {$member['id']} /  {$member['openid']}/{$member['nickname']}/{$member['realname']}/{$member['mobile']}");
            pdo_update('ewei_shop_member',$data, array('id' => $id, 'uniacid' => $_W['uniacid']));
            message('保存成功!', $this->createPluginWebUrl('commission/agent'), 'success');
        }
    } else {
        if ($operation == 'delete') {
            ca('commission.agent.delete');
            $id = intval($_GPC['id']);
            $member = pdo_fetch("select * from " . tablename('ewei_shop_member') . " where uniacid=:uniacid and id=:id limit 1 ", array(':uniacid' => $_W['uniacid'], ':id' => $id));
            if (empty($member)) {
                message('会员不存在，无法删除!', $this->createPluginWebUrl('commission/agent'), 'error');
            }
            $agentcount = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member') . ' where  uniacid=:uniacid and agentid=:agentid limit 1 ', array(':uniacid' => $_W['uniacid'], ':agentid' => $id));
            if ($agentcount > 0) {
                message('此会员有下线存在，无法删除!', '', 'error');
            }
            pdo_delete('ewei_shop_member', array('id' => $_GPC['id']));
            plog('commission.agent.delete', "删除分销商 <br/>分销商信息:  ID: {$member['id']} /  {$member['openid']}/{$member['nickname']}/{$member['realname']}/{$member['mobile']}");
            message('删除成功！', $this->createPluginWebUrl('commission/agent'), 'success');
        } else {
            if ($operation == 'user') {
                ca('commission.agent.user');
                $level = intval($_GPC['level']);
                $agentid = intval($_GPC['id']);
                $member = $this->model->getInfo($agentid);
                $total = $member['agentcount'];
                $level1 = $member['level1'];
                $level2 = $member['level2'];
                $level3 = $member['level3'];
                $condition = '';
                $params = array();
                if (empty($level)) {
                    $condition = " and ( dm.agentid={$member['id']}";
                    if ($level1 > 0) {
                        $condition .= " or  dm.agentid in( " . implode(',', array_keys($member['level1_agentids'])) . ")";
                    }
                    if ($level2 > 0) {
                        $condition .= " or  dm.agentid in( " . implode(',', array_keys($member['level2_agentids'])) . ")";
                    }
                    $condition .= ' )';
                    $hasagent = true;
                } else {
                    if ($level == 1) {
                        if ($level1 > 0) {
                            $condition = " and dm.agentid={$member['id']}";
                            $hasagent = true;
                        }
                    } else {
                        if ($level == 2) {
                            if ($level2 > 0) {
                                $condition = " and dm.agentid in( " . implode(',', array_keys($member['level1_agentids'])) . ")";
                                $hasagent = true;
                            }
                        } else {
                            if ($level == 3) {
                                if ($level3 > 0) {
                                    $condition = " and dm.agentid in( " . implode(',', array_keys($member['level2_agentids'])) . ")";
                                    $hasagent = true;
                                }
                            }
                        }
                    }
                }
                if (!empty($_GPC['mid'])) {
                    $condition .= ' and dm.id=:mid';
                    $params[':mid'] = intval($_GPC['mid']);
                }
                if (!empty($_GPC['realname'])) {
                    $_GPC['realname'] = trim($_GPC['realname']);
                    $condition .= ' and ( dm.realname like :realname or dm.nickname like :realname or dm.mobile like :realname)';
                    $params[':realname'] = "%{$_GPC['realname']}%";
                }
                if (empty($starttime) || empty($endtime)) {
                    $starttime = strtotime('-1 month');
                    $endtime = time();
                }
                if (!empty($_GPC['agentlevel'])) {
                    $condition .= ' and dm.agentlevel=' . intval($_GPC['agentlevel']);
                }
                if ($_GPC['parentid'] == '0') {
                    $condition .= ' and dm.agentid=0';
                } else {
                    if (!empty($_GPC['parentname'])) {
                        $_GPC['parentname'] = trim($_GPC['parentname']);
                        $condition .= ' and ( p.mobile like :parentname or p.nickname like :parentname or p.realname like :parentname)';
                        $params[':parentname'] = "%{$_GPC['parentname']}%";
                    }
                }
                if ($_GPC['followed'] != '') {
                    if ($_GPC['followed'] == 2) {
                        $condition .= ' and f.follow=0 and dm.uid<>0';
                    } else {
                        $condition .= ' and f.follow=' . intval($_GPC['followed']);
                    }
                }
                $pindex = max(1, intval($_GPC['page']));
                $psize = 20;
                $list = array();
                if ($hasagent) {
                    //$total = pdo_fetchcolumn("select count(dm.id) from" . tablename('ewei_shop_member') . " dm " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid" . " where dm.uniacid =" . $_W['uniacid'] . " and dm.isagent =1 and dm.status=1 {$condition}", $params);
                    //$list = pdo_fetchall("select dm.*,p.nickname as parentname,p.avatar as parentavatar  from " . tablename('ewei_shop_member') . " dm " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid  and f.uniacid={$_W['uniacid']}" . " where dm.uniacid = " . $_W['uniacid'] . " and dm.isagent =1 and dm.status=1 {$condition}   ORDER BY dm.agenttime desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
                    /*
                     *  gmm
                     *  修改：展示下级所有成员
                     *  时间：2015年11月26日
                     */
                    $total = pdo_fetchcolumn("select count(dm.id) from" . tablename('ewei_shop_member') . " dm " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid" . " where dm.uniacid =" . $_W['uniacid'] . "  {$condition}", $params);
                    $list = pdo_fetchall("select dm.*,p.nickname as parentname,p.avatar as parentavatar  from " . tablename('ewei_shop_member') . " dm " . " left join " . tablename('ewei_shop_member') . " p on p.id = dm.agentid " . " left join " . tablename('mc_mapping_fans') . "f on f.openid=dm.openid  and f.uniacid={$_W['uniacid']}" . " where dm.uniacid = " . $_W['uniacid'] . " {$condition}   ORDER BY dm.agenttime desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
                    $pager = pagination($total, $pindex, $psize);
                    foreach ($list as &$row) {
                        $info = $this->model->getInfo($row['openid'], array('total', 'pay'));
                        $row['levelcount'] = $info['agentcount'];
                        if ($this->set['level'] >= 1) {
                            $row['level1'] = $info['level1'];
                        }
                        if ($this->set['level'] >= 2) {
                            $row['level2'] = $info['level2'];
                        }
                        if ($this->set['level'] >= 3) {
                            $row['level3'] = $info['level3'];
                        }
                        $row['credit1'] = m('member')->getCredit($row['openid'], 'credit1');
                        $row['credit2'] = m('member')->getCredit($row['openid'], 'credit2');
                        $row['commission_total'] = $info['commission_total'];
                        $row['commission_pay'] = $info['commission_pay'];
                        $row['followed'] = m('user')->followed($row['openid']);
                        if ($row['agentid'] == $member['id']) {
                            $row['level'] = 1;
                        } else {
                            if (in_array($row['agentid'], array_keys($member['level1_agentids']))) {
                                $row['level'] = 2;
                            } else {
                                if (in_array($row['agentid'], array_keys($member['level2_agentids']))) {
                                    $row['level'] = 3;
                                }
                            }
                        }
                    }
                }
                unset($row);
                load()->func('tpl');
                include $this->template('agent_user');
                die;
            } else {
                if ($operation == 'clearagent') {
                    ca('commission.agent.clearagent');
                    $id = intval($_GPC['id']);
                    $member = pdo_fetch("select * from " . tablename('ewei_shop_member') . " where uniacid=:uniacid and id=:id limit 1 ", array(':uniacid' => $_W['uniacid'], ':id' => $id));
                    if (empty($member)) {
                        message('会员不存在，无法清除!', $this->createPluginWebUrl('commission/agent'), 'error');
                    }
                    $data = array(
                        'agentid' => ''
                    );
                    pdo_update('ewei_shop_member', $data, array('id' => $id, 'uniacid' => $_W['uniacid']));
                    plog('commission.agent.clearagent', "清除分销商与上级关系 <br/>分销商信息:  ID: {$member['id']} /  {$member['openid']}/{$member['nickname']}/{$member['realname']}/{$member['mobile']}");
                    message('清除成功！', $this->createPluginWebUrl('commission/agent'), 'success');
                } else {
                    if ($operation == "agentshop") {
                        $pindex = max(1, intval($_GPC['page']));
                        $psize = 20;
                        $params = array();
                        $condition = '';
                        if (!empty($_GPC['mid'])) {
                            $condition .= 'and a.mid=:mid';
                            $params[':mid'] = intval($_GPC['mid']);
                        }
                        if (!empty($_GPC['realname'])) {
                            $_GPC['realname'] = trim($_GPC['realname']);
                            $condition .= ' and ( m.realname like :realname or m.nickname like :realname or m.mobile like :realname)';
                            $params[':realname'] = "%{$_GPC['realname']}%";
                        }
                        if (!empty($_GPC['shoptype'])) {
                            $condition .= ' and m.shoptype=:shoptype ';
                            $params[':shoptype'] = intval($_GPC['shoptype']);
                        }
                        if (!empty($_GPC['isShop']) || $_GPC['isShop']==="0") {
                            $condition .= ' and m.isShop=:isShop ';
                            $params[':isShop'] = intval($_GPC['isShop']);
                        }
                        $params[':uniacid'] = $_W['uniacid'];
                        $_GPC['method'] = "agentshop";
                        $list = pdo_fetchall("select a.mid,m.avatar,m.nickname,m.realname,m.mobile,m.isShop,a.opentime,m.shoptype from " . tablename('ewei_shop_agent') . " a left join ".tablename('ewei_shop_member')."m on a.mid=m.id where a.uniacid=:uniacid  {$condition}  order by a.id desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
                        $total = pdo_fetchcolumn("select count(*) from " . tablename('ewei_shop_agent') . "a left join ".tablename('ewei_shop_member')."m  on a.mid=m.id where a.uniacid=:uniacid  {$condition}  order by a.id desc ", $params);
                        load()->func('tpl');
                        $pager = pagination($total, $pindex, $psize);
                        if($_GPC['export']==1){
                                $list = pdo_fetchall("select a.mid,m.avatar,m.nickname,m.realname,m.mobile,m.isShop,a.opentime,m.shoptype from " . tablename('ewei_shop_agent') . " a left join ".tablename('ewei_shop_member')."m on a.mid=m.id where a.uniacid=:uniacid  {$condition}  order by a.id desc", $params);
                                $columns = array(
                                array('title' => '店铺MID', 'field' => 'mid', 'width' => 12),
                                array('title' => '店铺类型', 'field' => 'shoptype', 'width' => 12), 
                                array('title' => '昵称', 'field' => 'nickname', 'width' => 24),
                                array('title' => '姓名', 'field' => 'realname', 'width' => 24),
                                array('title' => '手机号码', 'field' => 'mobile', 'width' => 24),
                                array('title' => '开启时间', 'field' => 'opentime', 'width' => 24),
                                array('title' => '状态', 'field' => 'isShop', 'width' =>12),
                                );
                               foreach($list as $k=>$v){
                                 $r['mid']=$v['mid'];
                                 $r['nickname']=$v['nickname'];
                                 $r['realname']=$v['realname'];
                                 if($v['shoptype']==1){$r['shoptype']="银冠店"; }
                                 if($v['shoptype']==2){$r['shoptype']="金冠店";  }
                                 if($v['shoptype']==3){$r['shoptype']="钻石店";  }
                                 $r['mobile']=$v['mobile'];
                                 if(!empty($v['opentime'])){
                                 $r['opentime']=date('Y-m-d H:i',$v['opentime']);
                                 }else{
                                 $r['opentime']="";  
                                 }
                                 if($v['isShop']==1){
                                     $r['isShop']="开启";
                                 }else{
                                     $r['isShop']="关闭";
                                 }
                                 $exportlist[] = $r;
                               }
                               unset($r);
                               m('excel')->export($exportlist, array("title" => "店铺管理数据-" . date('Y-m-d-H-i', time()), "columns" => $columns));
                        }
                        include $this->template('agentshop');
                        die;
                    } else {
                        if ($operation == "order") {
                            $_GPC['method'] = "order";
                            //开通小店店铺
                            if ($_GPC['open']) {
                                $data = pdo_fetch("select a.id as recordid,b.id,a.ordersn,a.openid,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,b.weixin,b.openid,b.isShop from " . tablename('ewei_shop_commission_apply_order') . "as a  left join  " . tablename('ewei_shop_member') . "as b on a.openid=b.openid where a.uniacid=:uniacid  and a.id=:id  ", array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['open']));
                                $user = pdo_fetch("select * from " . tablename('ewei_shop_agent') . "  where uniacid=:uniacid  and mid=:mid  ", array(':uniacid' => $_W['uniacid'], ':mid' => $data['id']));
                                if ($user) {
                                    if ($data['isShop'] == 0) {
                                        pdo_update("ewei_shop_member", array("isShop" =>1,'shoptype'=>$data['type'],'agenttime'=>time()), array('uniacid' => $_W['uniacid'], 'openid' => $data['openid']));
                                        pdo_update("ewei_shop_agent", array("opentime" =>time()), array('uniacid' => $_W['uniacid'], 'mid' => $data['id']));
                                    }
                                }else{
                                    $shop = array(
                                        'uniacid' => $_W['uniacid'],
                                        'mid' => $data['id'],
                                        'createtime' => time(),
                                        'opentime' => time()
                                    );
                                    pdo_update("ewei_shop_member", array("isShop" =>1,'shoptype'=>$data['type'],'agenttime'=>time()), array('uniacid' => $_W['uniacid'], 'openid' => $data['openid']));
                                    pdo_insert("ewei_shop_agent", $shop);
                                }
                                $xiaoshop = pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $data['id']));
                                $shopdata['selectgoods'] = 1;
                                $shopdata['selectcategory'] = 0;
                                $shopdata['uniacid'] = $_W['uniacid'];
                                $shopdata['mid'] = $data['id'];
                                if (empty($xiaoshop['id'])) {
                                    pdo_insert('ewei_shop_commission_shop', $shopdata);
                                } else {
                                    pdo_update('ewei_shop_commission_shop', $shopdata, array('id' => $xiaoshop['id']));
                                }
                                if(!empty($data['id'])){
                                pdo_update('ewei_shop_member', array('isagent' => 1, 'status' =>1,'agentlevel'=>3,"dongfan"=>0), array('id' => $data['id'], 'uniacid' => $_W['uniacid']));
                                pdo_update("ewei_shop_commission_apply_order", array('sendtime' => time(), 'status' => 2), array('id' => $_GPC['open']));
                                }
                                if ($data['type'] == 1) {
                                    $data_type = "银冠店";
                                }
                                if ($data['type'] == 2) {
                                    $data_type = "金冠店";
                                }
                                if ($data['type'] == 3) {
                                    $data_type = "钻石店";
                                }
                                $msg = array(
                                    'first' => array('value' => "云店审核通过通知！", "color" => "#4a5077"),
                                    'username' => array('title' => '申请人', 'value' => $data['nickname'], "color" => "#4a5077"),
                                    'orderProductPrice' => array('title' => '支付金额', 'value' => '￥' . $data['price'] . '元', "color" => "#4a5077"),
                                    'orderProductName' => array('title' => '店铺类型', 'value' => $data_type, "color" => "#4a5077"),
                                    'paytime' => array('title' => '审核时间', 'value' => date('Y-m-d H:i:s', time()), "color" => "#4a5077"),
                                    'remark' => array('value' => "\r\n恭喜您的云店申请已审核通过，专属功能请在“个人中心”——“分销中心”中查看!", "color" => "#4a5077"),
                                     'remark2' => array('value' => "添加小妖客服微信号：lanxuevip2016  提供一对一咨询服务！", "color" => "#4a5077")
                                );
                                m('message')->sendCustomNotice($data['openid'], $msg);
                                message('审核成功!',referer(), 'success');
                            }
                            $pindex = max(1, intval($_GPC['page']));
                            $psize = 20;
                            $params = array();
                            $condition = '';
                            if(empty($_GPC['time'])){
                            $starttime = strtotime('-1 month');
                            $endtime = time();
                            }else{
                            $starttime = strtotime($_GPC['time']['start']);
                            $endtime = strtotime($_GPC['time']['end']); 
                            }
                            if($_GPC['searchtime']==1){
                                $condition .= " AND  a.createtime >= :starttime AND  a.createtime <= :endtime ";
                                $params[':starttime'] = $starttime;
                                $params[':endtime'] = $endtime;
                            }
                            if (!empty($_GPC['realname'])) {
                                $_GPC['realname'] = trim($_GPC['realname']);
                                $condition .= ' and ( b.realname like :realname or b.nickname like :realname or b.mobile like :realname)';
                                $params[':realname'] = "%{$_GPC['realname']}%";
                            }
                            if (!empty($_GPC['tid'])) {
                                $condition .= 'and a.ordersn like :tid';
                                $params[':tid'] = "%{$_GPC['tid']}%";
                            }
                            if (!empty($_GPC['status'])) {
                                $condition .= 'and a.status like :status';
                                $params[':status'] = "{$_GPC['status']}";
                            }
                            
                            $params[':uniacid'] = $_W['uniacid'];
                            load()->func('tpl');
                            $list = pdo_fetchall("select a.id,a.ordersn,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,c.fee,c.type as pay_type from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid LEFT JOIN ".tablename('ewei_shop_commission_apply_paylog')." as c on a.ordersn=c.tid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
                            $total = pdo_fetchcolumn("select count(*) from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc ", $params);
                            $pager = pagination($total, $pindex, $psize);
                            if($_GPC['export']==1){
                                $list = pdo_fetchall("select a.id,a.ordersn,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,c.fee,c.type as pay_type from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid LEFT JOIN ".tablename('ewei_shop_commission_apply_paylog')." as c on a.ordersn=c.tid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc", $params);
                                $columns = array(
                                array('title' => '编号', 'field' => 'ordersn', 'width' => 24),
                                array('title' => '呢称', 'field' => 'nickname', 'width' => 24), 
                                array('title' => '店铺类型', 'field' => 'type', 'width' => 24),
                                array('title' => '支付类型', 'field' => 'pay_type', 'width' => 24),
                                array('title' => '支付金额', 'field' => 'fee', 'width' => 24),
                                array('title' => '姓名', 'field' => 'realname', 'width' => 24),
                                array('title' => '手机号码', 'field' => 'mobile', 'width' => 24),
                                array('title' => '付款时间', 'field' => 'createtime', 'width' => 24),
                                array('title' => '审核时间', 'field' => 'sendtime', 'width' => 24),
                                array('title' => '状态', 'field' => 'status', 'width' => 24),
                                );
                               foreach($list as $k=>$v){
                                 $r['ordersn']=$v['ordersn'];
                                 $r['nickname']=$v['nickname'];
                                 if($v['type']==1){
                                    $r['type']="银冠店"; 
                                 }
                                 if($v['type']==2){
                                   $r['type']="金冠店";  
                                 }
                                 if($v['type']==3){
                                   $r['type']="钻石店";  
                                 }
                                 if($v['pay_type']=='wechat'){
                                   $r['pay_type']='微信支付';
                                 }
                                 if($v['pay_type']=='unionpay'){
                                   $r['pay_type']='银联支付';
                                 }
                                 if($v['pay_type']=='cash'){
                                   $r['pay_type']='余额支付';
                                 }
                                 $r['fee']=$v['fee']."元";
                                 $r['realname']=$v['realname'];
                                 $r['mobile']=$v['mobile'];
                                 $r['createtime']=date('Y-m-d H:i',$v['createtime']);
                                 $r['sendtime']=date('Y-m-d H:i',$v['sendtime']);
                                 if($v['status']==1){
                                     $r['status']="待审核";
                                 }else{
                                     $r['status']="已审核";
                                 }
                                 $exportlist[] = $r;
                               }
                               unset($r);
                               m('excel')->export($exportlist, array("title" => "店铺审核数据-" . date('Y-m-d-H-i', time()), "columns" => $columns));
                            }
                            include $this->template('order');
                            die;
                        } else {
                            if ($operation == "paylog") {
                                $pindex = max(1, intval($_GPC['page']));
                                $psize = 20;
                                $params = array();
                                $condition = '';
                                if (!empty($_GPC['realname'])) {
                                    $_GPC['realname'] = trim($_GPC['realname']);
                                    $condition .= ' and ( b.realname like :realname or b.nickname like :realname or b.mobile like :realname)';
                                    $params[':realname'] = "%{$_GPC['realname']}%";
                                }
                                if (!empty($_GPC['tid'])) {
                                    $condition .= 'and a.tid like :tid';
                                    $params[':tid'] = "%{$_GPC['tid']}%";
                                }
                                $params[':uniacid'] = $_W['uniacid'];
                                $_GPC['method'] = "paylog";
                                load()->func('tpl');
                                $list = pdo_fetchall("select a.type,a.fee,a.tid,c.createtime,b.realname,b.nickname,b.mobile,b.avatar from " . tablename('ewei_shop_commission_apply_paylog') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.uid left join " . tablename('ewei_shop_commission_apply_order') . "as c on a.tid=c.ordersn  where a.uniacid=:uniacid and a.status=1  {$condition}  order by a.plid desc ", $params);
                                $total = pdo_fetchcolumn("select count(*) from " . tablename('ewei_shop_commission_apply_paylog') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.uid where a.uniacid=:uniacid and a.status=1  {$condition}  order by a.plid desc ", $params);
                                $pager = pagination($total, $pindex, $psize);
                                include $this->template('paylog');
                                die;
                            }
                        }
                    }
                }
            }
        }
    }
}
load()->func('tpl');
include $this->template('agent');
